package com.tubruk.todo;

import java.text.DateFormat;
import java.util.Calendar;

import com.tubruk.todo.activity.R;
import com.tubruk.todo.activity.TodoActivity;

import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tubruk.todo.database.TodoAdapter;

public class TaskContentCursorAdapter extends CursorAdapter {
	LayoutInflater inflater;
	
	private static String TEXT_DUE_DATE = "Due Date: ";
	private OnClickHandler onClickHandler = null;
	private OnTickHandler onTickHandler = null;
	
	private static int PRIORITY_COLOR_HIGH = Color.argb(200, 255, 0, 0);
	private static int PRIORITY_COLOR_MEDIUM = Color.argb(200, 255, 255, 0);
	private static int PRIORITY_COLOR_LOW = Color.argb(200, 0, 255, 0);
	private static int PRIORITY_COLOR_NONE = Color.argb(200, 0, 0, 0);
	
	public TaskContentCursorAdapter(Context context, Cursor c) {
		super(context, c);
		inflater = LayoutInflater.from(context);
		onClickHandler = new OnClickHandler();
		onTickHandler = new OnTickHandler();
	}
	
	private TodoActivity activity = null;
	
	
	//nasty >_>
	public void setActivity(TodoActivity activity){
		this.activity = activity;
	}
 
	public void bindView(View view, Context context, Cursor cursor) {
		//bindView(view, context, cursor);
		whyDoINeedThisMethod(view, cursor);
		
	}
	
	//the ugly way
	private class OnClickHandler implements View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.d("TEST", "TESTONCLICK");
			AlertDialog.Builder adb=new AlertDialog.Builder(activity);
			adb.setTitle("LVSelectedItemExample");
			adb.setMessage("NASTY! onItemClickListener does not work if there is any focusable view in the list. Popup from TaskContentCursorAdapter" + v.getId());
			adb.setPositiveButton("Ok", null);
			adb.show();
		}
		
	}
	
	private class OnTickHandler implements View.OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			CheckBox cb = (CheckBox) v;
			int done = TaskContent.DONE_NO;
			if (cb.isChecked()){
				done = TaskContent.DONE_YES;
			}
			TodoAdapter.updateComplete(TaskContentCursorAdapter.this.activity.getContentResolver(), done, cb.getId());
		}
		
	}
	
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View v = inflater.inflate(R.layout.dataview, parent, false);
		//bindView(v, context, cursor);
		
		whyDoINeedThisMethod(v, cursor);
		
		return v;
	}
	
	public void whyDoINeedThisMethod(View v, Cursor cursor){
		v.setOnClickListener(onClickHandler);
		int id = cursor.getInt(cursor.getColumnIndex(Todos.Todo._ID));
		
		v.setId(id);
		TextView task = (TextView) v.findViewById(R.id.task);
		if (task != null){
			task.setText(cursor.getString(cursor.getColumnIndex(Todos.Todo.TITLE)));
		}
		
		String dueDateStr = cursor.getString(cursor.getColumnIndex(Todos.Todo.DUE_DATE));
		if (dueDateStr != null){
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(Long.valueOf(dueDateStr));
			
			TextView dueDate = (TextView) v.findViewById(R.id.dueDate);
			if (dueDate != null){
				dueDate.setText(TaskContentCursorAdapter.TEXT_DUE_DATE + 
						DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT)
						.format(calendar.getTime())
				);
			}
		}
		
		int priority = cursor.getInt(cursor.getColumnIndex(Todos.Todo.PRIORITY));
		
		if (priority != TaskContent.PRIORITY_NONE){
			ImageView priorityView = (ImageView) v.findViewById(R.id.priorityView);
			
			if (priorityView != null){
				int bgColor = TaskContentCursorAdapter.PRIORITY_COLOR_NONE; 
				if (priority == TaskContent.PRIORITY_LOW) { 
						bgColor = TaskContentCursorAdapter.PRIORITY_COLOR_LOW;
				}else if (priority == TaskContent.PRIORITY_MEDIUM){ 
						bgColor = TaskContentCursorAdapter.PRIORITY_COLOR_MEDIUM;
				}else if (priority == TaskContent.PRIORITY_HIGH) { 
						bgColor = TaskContentCursorAdapter.PRIORITY_COLOR_HIGH; 
				}
				priorityView.setBackgroundColor(bgColor);
			}
		}
		
		CheckBox doneCB = (CheckBox) v.findViewById(R.id.doneCB);
		if (doneCB != null ){
			doneCB = (CheckBox) v.findViewById(R.id.doneCB);
			
			int hasDone = cursor.getInt(cursor.getColumnIndex(Todos.Todo.DONE));
			if (hasDone == TaskContent.DONE_YES){
				doneCB.setChecked(true);
			}
			doneCB.setOnClickListener(onTickHandler);
			doneCB.setId(id);
		}
	}

}
